package ocean.chinacraft.common.entity.goal;

import net.minecraft.entity.EntityPredicate;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.TargetGoal;
import net.minecraft.entity.player.PlayerEntity;
import ocean.chinacraft.common.world.item.armor.SilkNightArmor;
import ocean.chinacraft.common.util.ModUtil;

import java.util.List;

/**
 * 自定义目标
 *
 * @author zhang
 * @since 2021/12/13
 */
public class MonsterAttackGoal extends TargetGoal {

    private static final EntityPredicate PREDICATE =
            (new EntityPredicate()).allowUnseeable().ignoreInvisibilityTesting();

    public MonsterAttackGoal(MobEntity entity, boolean mustSee) {
        super(entity, mustSee);
    }

    @Override
    public boolean canUse() {
        return true;
    }

    @Override
    public void tick() {
        LivingEntity target = mob.getTarget();
        if (target instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) target;
            if (SilkNightArmor.invalid(player)) {
                List<MobEntity> mobEntities = ModUtil.getNearbyEntities(player, 20, PREDICATE);
                for (MobEntity mob : mobEntities) {
                    if (mob.getTarget() != null && mob.getTarget().getUUID().equals(player.getUUID())) {
                        mob.setTarget(null);
                    }
                }
            }
        }
    }
}
